#include "iConfig.hpp"

#include <glf/core/str.h>

#include <cctype>

#if __APPLE__
	#include "str_posix_.hpp"
#else
	#include GLFi_HEADER_API(str, GLF_API_STR)
#endif

#include "str_gl_.hpp"

namespace glf {

char ToLower(char c) {
	return std::tolower(c);
}

const char* Stristr(const char* str, const char* subStr) {
    if(!*subStr) {
        return str;
	}

    const char *cp = str;
    while(*cp) {
        const char* s1 = cp;
        const char* s2 = subStr;

        while(*s1 && *s2 && !(glf::ToLower(*s1) - glf::ToLower(*s2))) {
			s1++;
			s2++;
		}

        if (!*s2)
			return cp;

        cp++;
    }

    return 0;
}

template < typename T_STR >
void ToLowerCase( T_STR& str, int len, int startoffset, int endoffset) {
	endoffset = (endoffset == -1) ? len : endoffset;
	if( endoffset >= len) {
		endoffset = len - 1;
	}
	
	for( int i = startoffset; i <= endoffset; i++) {
		str[i] = ToLower(str[i]);		
	}
}

void ToLowerCase( char* str, int startoffset, int endoffset) {	
	return ToLowerCase<char*>(str, glf::Strlen(str), startoffset, endoffset);	
}

void ToLowerCase( std::string& str, int startoffset, int endoffset) {	
	return ToLowerCase<std::string>(str, str.size(), startoffset, endoffset);	
}

}